package com.example.administrator.myapplication;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.SeekBar;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/5/22.
 */

public class CustomService extends Service {

    private MediaPlayer mediaPlayer;
    private CustomBroadcast customBroadcast;
    private NotificationManager notificationManager;

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = new MediaPlayer();

        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        initReceive();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String action = intent.getAction();
        switch (action) {
            case "STARTMUSICE":
                Log.e("test1", "start_music");
                Intent intent1 = new Intent();
                intent1.setAction("start");
                sendBroadcast(intent1);
                break;

            case "STOPMUSICE":
                Log.e("test2", "stop_music");
                Intent intent2 = new Intent("stop");
                sendBroadcast(intent2);
                break;

            case "PAUSEMUSICE":
                Log.e("test3", "pause_music");
                Intent intent3 = new Intent("pause");
                sendBroadcast(intent3);
                break;

            case "STARTMUSICE1":
                Log.e("test7", "start_music1");
                play();
                break;

            case "PAUSEMUSICE1":
                Log.e("test8", "start_music2");
                pause();
                break;

            case "STOPMUSICE1":
                Log.e("test9", "start_music3");
                break;

        }

        return super.onStartCommand(intent, flags, startId);
    }

    class MyBinder extends Binder {
        CustomService getService() {
            return CustomService.this;
        }
    }

    private SeekBar mySeekBar;

    public void getSeekBar(SeekBar seekBar) {
        mySeekBar = seekBar;
    }

    private Runnable myRunnable = new Runnable() {
        @Override
        public void run() {
            int position = mediaPlayer.getCurrentPosition();
            int duration = mediaPlayer.getDuration();
            if (duration > 0) {
                // 计算进度（获取进度条最大刻度*当前音乐播放位置 / 当前音乐时长）
                long pos = mySeekBar.getMax() * position / duration;
                Log.e("test222", pos + "..." + duration);
                mySeekBar.setProgress((int) pos);
            }
            handler.postDelayed(myRunnable, 1000);
        }
    };

    Handler handler = new Handler();


    private void pause() {
        isPause = true;
        handler.removeCallbacks(myRunnable);
        mediaPlayer.pause();
    }

    private boolean isPause;

    private void play() {
        if(!mediaPlayer.isPlaying() && isPause == false){
            Log.e("asd","isPause.."+isPause);
            isPause = true;
            try {
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mediaPlayer.setDataSource("http://abv.cn/music/%E5%8D%83%E5%8D%83%E9%98%99%E6%AD%8C.mp3");
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
                    @Override
                    public void onBufferingUpdate(MediaPlayer mp, int percent) {
                        mySeekBar.setSecondaryProgress(percent);
                        int currentProgress = mySeekBar.getMax()
                                * mediaPlayer.getCurrentPosition() / mediaPlayer.getDuration();
                        Log.e(currentProgress + "% play", percent + " buffer");
                    }
                });
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mediaPlayer.start();
                        handler.postDelayed(myRunnable, 1000);
                    }
                });
                mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        Log.e("test11", "end...");
                        mediaPlayer.pause();
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            Log.e("asd22","isPause.."+isPause);
            isPause = false;
            mediaPlayer.start();
            handler.postDelayed(myRunnable, 1000);
        }



    }

    private void initReceive() {
        customBroadcast = new CustomBroadcast();
        IntentFilter intentFilter1 = new IntentFilter();
        intentFilter1.addAction("start");
        registerReceiver(customBroadcast, intentFilter1);

        IntentFilter intentFilter2 = new IntentFilter();
        intentFilter2.addAction("stop");
        registerReceiver(customBroadcast, intentFilter2);

        IntentFilter intentFilter3 = new IntentFilter();
        intentFilter3.addAction("pause");
        registerReceiver(customBroadcast, intentFilter3);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }
}
